# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=emacs-emacsql
pkgver=3.1.1_git20240714
_gitrev=8aeffcfc3f5700af7a6216889610330cb438d3b9
pkgrel=0
_pkgreal=emacsql
pkgdesc="High-level SQL database frontend"
url="https://github.com/magit/emacsql"
arch="all"
license="Unlicense"
depends="cmd:emacs"
makedepends="emacs-nox sqlite-dev"
checkdepends="emacs-sqlite3-api"
subpackages="
	$pkgname-mysql::noarch
	$pkgname-psql::noarch
	$pkgname-sqlite-module:sqlite_module:noarch
	$pkgname-sqlite
	"
source="https://github.com/magit/emacsql/archive/$_gitrev/emacs-emacsql-$_gitrev.tar.gz
	compile-against-system-libsqlite3-and-use-Debian-CFLAGS.patch
	compile-against-system-sqlite3-and-don-t-assume-foreign-k.patch
	fix-test-emacsql-sqlite-executable-path.patch
	set-all-Makefile-target-to-build-what-we-want.patch
	use-system-emacsql-sqlite-binary.patch
	"
builddir="$srcdir/$_pkgreal-$_gitrev"

prepare() {
	default_prepare

	# Requires unpackaged pg-el & peg
	rm -v emacsql-pg.el
}

build() {
	make

	emacs -batch -q -no-site-file -L . \
		-eval '(setq byte-compile-error-on-warn t)' \
		-f batch-byte-compile \
		./*.el
}

check() {
	emacs -batch -q -no-site-file -L . -L tests \
		-l emacsql-tests -f ert-run-tests-batch-and-exit
}

package() {
	install -Dvm755 sqlite/emacsql-sqlite -t "$pkgdir"/usr/bin/

	install -Dvm644 ./*.el ./*.elc \
		-t "$pkgdir"/usr/share/emacs/site-lisp/$_pkgreal/

	cd "$pkgdir"/usr/share/emacs/site-lisp/$_pkgreal
	emacs -batch -q -no-site-file \
		-eval "(loaddefs-generate \".\" \"$_pkgreal-loaddefs.el\")"
}

mysql() {
	pkgdesc="$pkgdesc (MySQL backend)"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove usr/share/emacs/site-lisp/$_pkgreal/emacsql-mysql*
}

psql() {
	pkgdesc="$pkgdesc (PostgreSQL backend)"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove usr/share/emacs/site-lisp/$_pkgreal/emacsql-psql*
}

sqlite_module() {
	pkgdesc="$pkgdesc (SQLite3 API backend)"
	depends="
		$pkgname-sqlite=$pkgver-r$pkgrel
		emacs-sqlite3-api
		"

	amove usr/share/emacs/site-lisp/$_pkgreal/emacsql-sqlite-module*
}

sqlite() {
	pkgdesc="$pkgdesc (SQLite backends)"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove usr/bin/emacsql-sqlite \
		usr/share/emacs/site-lisp/$_pkgreal/emacsql-sqlite*
}

sha512sums="
d68d3e0f486a9462e187d8955b4fc800a98f8603e1eaf3eae95a38b25eafe993bc30390970b3cbc9f7f60f46e535c309efd45667e0f26f9893d0c5e2207898d3  emacs-emacsql-8aeffcfc3f5700af7a6216889610330cb438d3b9.tar.gz
270c5914b3a0b01dad529ab8813b4d8b2955424d64531f17b43eef1a76c8971df6d571becb6deb50de8f46ee9b51b94dda4bd98e4b2875f973e92a803a715e58  compile-against-system-libsqlite3-and-use-Debian-CFLAGS.patch
9d07889d38a055e2ad5aa332ac2d9d489bfcf9cad9bd9b73d7f213697c0aa71ae592522af3579412dd763bb3782c634b35be9ebb56f9597339284a0076f2de22  compile-against-system-sqlite3-and-don-t-assume-foreign-k.patch
19fde52185028f3e6edf97619947e497ed101b3200a68dbbf1413dbf44725f9f4ab6cfc0cf2497cc8132c87f2b764798d1b07433c7bf1309415d7650d3c6e309  fix-test-emacsql-sqlite-executable-path.patch
a1544678f3506d77925790d7c48f118952d94f79bf5d730f1a5b3ff73146d8bc112a14cd9caf9e5dc3df44969132c92e6961cb5f110a31d903a6434c85b8ee2c  set-all-Makefile-target-to-build-what-we-want.patch
a95030be49e95679b6b7ffa350cd56c66040369e6cd4787ab00d46045e36a39b9999e9fe26deadac3b0b0f88aac7c1bae874e208bc721657ffdd9d0e4fd779ab  use-system-emacsql-sqlite-binary.patch
"
